package com.joysuch.wwyt.patrol.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.patrol.entity.bean.PatrolCheckSearchBean;
import com.joysuch.wwyt.patrol.entity.bean.PatrolDailySearchBean;
import com.joysuch.wwyt.patrol.entity.bean.StatPlanTitleSearchBean;
import com.joysuch.wwyt.patrol.entity.bean.StatTrendSearchBean;
import com.joysuch.wwyt.patrol.entity.dto.PatrolCheckReportDto;
import com.joysuch.wwyt.patrol.entity.dto.StatPlanTitleDto;
import com.joysuch.wwyt.patrol.entity.vo.PatrolCheckReportVo;
import com.joysuch.wwyt.patrol.entity.vo.PatrolDailyReportVo;
import com.joysuch.wwyt.patrol.entity.vo.StatTrendBean;
import com.joysuch.wwyt.patrol.service.PatrolStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/patrol/stat")
@Api(tags = "智能巡检任务")
@AllArgsConstructor
public class PatrolStatisticsController {

    private final PatrolStatisticsService patrolStatisticsService;

    @GetMapping("list")
    @ApiOperation(value = "日报统计列表")
    public ResultBean dailyReportPage(@Valid PatrolDailySearchBean searchBean) {
        PatrolDailyReportVo reportVo = patrolStatisticsService.dailyReportPage(searchBean);
        return ResultBean.success(reportVo);
    }

    @GetMapping("export")
    @ApiOperation(value = "统计列表导出")
    public void exportStat(PatrolDailySearchBean searchBean, HttpServletRequest request, HttpServletResponse
            response) {
        try {
            patrolStatisticsService.exportDailyReport(searchBean, request, response);
        } catch (IOException e) {
            log.error("统计列表导出", e);
        }
    }

    @GetMapping("plan-stat")
    @ApiOperation(value = "巡检统计")
    public ResultBean checkReportPage(@Valid PatrolCheckSearchBean searchBean) {
        Page<PatrolCheckReportDto> page = patrolStatisticsService.checkReportPlanStat(searchBean);
        return ResultBean.pageData(page.getRecords(), page.getTotal());
    }

    @GetMapping("plan-page")
    @ApiOperation("计划抬头分页")
    public ResultBean getPlanPage(StatPlanTitleSearchBean searchBean) {
        Page<StatPlanTitleDto> planTitle = patrolStatisticsService.getPlanTitle(searchBean);
        return ResultBean.pageData(planTitle.getRecords(),planTitle.getTotal());
    }


    @GetMapping("plan-detail")
    @ApiOperation(value = "计划详情")
    public ResultBean planDetail(StatPlanTitleSearchBean searchBean) {
        PatrolCheckReportVo checkReportVo = patrolStatisticsService.planDetail(searchBean);
        return ResultBean.success(checkReportVo);
    }

    @GetMapping("get-trend")
    @ApiOperation(value = "趋势图")
    public ResultBean getTrend(StatTrendSearchBean searchBean) {
        StatTrendBean trend = patrolStatisticsService.getTrend(searchBean);
        return ResultBean.success(trend);
    }
}

